package chessmate;

import java.util.ArrayList;
import java.util.Random;

public class startGame {

	public static void main(String [] args){
//		playOnClient();
		playOnLocalMachine();
//		humanVsMachine();
	}//End main
	
	public static void humanVsMachine(){
		char outcome = 'a';
		String yourMove;
		Random ran = new Random();
		
		State state = new State();
		
		do{
			
			System.out.println();
			System.out.println(state.onMove + " " + state.moveNum);
			
			for(int i=0; i < state.board.length; i++){
				for(int j=0; j < state.board[i].length; j++){
					System.out.print(state.board[i][j]);
				}//end for j
				System.out.println();
			}//end for i
			
			System.out.println(state.onMove + " " + state.moveNum);
			
			if(state.onMove == 'w'){
				System.out.println("Your Move: ");
				yourMove = TerminalInput.readString();
				Move move = new Move(yourMove);
				outcome = state.move(move.from, move.to);
				System.out.println(move.from + " " + move.to);
			}else{
				state.time = System.currentTimeMillis();
				Move move = callBestMove(state);
//				Move move = callNegamax(state, ran);
//				Move move = randomMove(state, ran);
				outcome = state.move(move.from, move.to);
				System.out.println(move.from + " " + move.to);
			}//end if else
			
			for(int i=0; i < state.board.length; i++){
				for(int j=0; j < state.board[i].length; j++){
					System.out.print(state.board[i][j]);
				}//end for j
				System.out.println();
			}//end for i
			
			switch(outcome){
			case 'w':
				System.out.println("White Player wins!");
				break;
			case 'b':
				System.out.println("Black Player wins!");
				break;
			case '-':
				System.out.println("The Game was a draw!");
				break;
			}//end switch case
			
		}while(outcome == 'a');
	}//end method
	
	public static void playOnLocalMachine(){
		char outcome = 'a';
		Random ran = new Random();
		
		//new State
		State state = new State();
		
		do{
			System.out.println();
			System.out.println(state.onMove + " " + state.moveNum);
			
//			if(state.onMove == 'w'){
//			Move move = callNegamax(state, ran);
//			Move move = randomMove(state, ran);
//			outcome = state.move(move.from, move.to);
//			System.out.println(move.from + " " + move.to);
//			}
//			else{
//			
			//new
			state.time = System.currentTimeMillis();
			
				Move move = callBestMove(state);
			
//				String m = callBestMove(state);
//				if(m == "resign")
//					System.exit(-1);
//				Move move = new Move(m);
//				Move move = callNegamax(state, ran);
//				Move move = randomMove(state, ran);
				outcome = state.move(move.from, move.to);
				System.out.println(move.from + " " + move.to);
//			}
			for(int i=0; i < state.board.length; i++){
				for(int j=0; j < state.board[i].length; j++){
					System.out.print(state.board[i][j]);
				}//end for j
				System.out.println();
			}//end for i
			
			switch(outcome){
			case 'w':
				System.out.println("White Player wins!");
				break;
			case 'b':
				System.out.println("Black Player wins!");
				break;
			case '-':
				System.out.println("The Game was a draw!");
				break;
			}//end switch case
			
		}while(outcome == 'a');
	}//end playOnLocalMachine
	
	public static void playOnClient(){
		String server = "imcs.svcs.cs.pdx.edu";
		String portStr = "3589";
		String username = "Marco";
		String password = "1234";
		String select;
		String opponentsMove;
		char color = 'w';
		char outcome = 'a';
		Random ran = new Random();
		
//		System.out.print("Username: ");
//		username = TerminalInput.readString();
//		System.out.println();
//		System.out.print("Password: ");
//		password = TerminalInput.readString();
//		System.out.println();
		System.out.print("Offer or accept? ");
		select = TerminalInput.readString();
		System.out.println();
		System.out.print("Choose your Color: ");
		String tmp = TerminalInput.readString();
		color = tmp.charAt(0);
		System.out.println();
		
		try{
			Client client = new Client(server, portStr, username, password);
			
			if(select.equalsIgnoreCase("offer")){
				client.offer(color);
				System.out.println("Game offered");
			}//end if
			
			if(select.equalsIgnoreCase("accept")){
				System.out.print("Enter your GameID: ");
				String gameID = TerminalInput.readString();
				System.out.println("game id '" + gameID + "'");
				client.accept(gameID, color);
			}//end if
			
			//new State
			State state = new State();
			
			do{
				for(int i=0; i < state.board.length; i++){
					for(int j=0; j < state.board[i].length; j++){
						System.out.print(state.board[i][j]);
					}//end for j
					System.out.println();
				}//end for i
				
				System.out.println(state.onMove + " " + state.moveNum);
				
				//own turn
				if(state.onMove == color){
					state.time = System.currentTimeMillis();
					Move move = callBestMove(state);
//					Move move = callNegamax(state, ran);
//					Move move = randomMove(state, ran);
					System.out.println(move.toString());
					client.sendMove(move.from + "-" + move.to);
					outcome = state.move(move.from, move.to);
				}else{
					//opponents turn
					opponentsMove = client.getMove();
					
					if(opponentsMove == null){
						System.out.println("Game Over!");
						if(state.onMove == 'w')
							outcome = 'w';
						if(state.onMove == 'b')
							outcome = 'b';
					}else{
						Move move = new Move(opponentsMove);
						outcome = state.move(move.from, move.to);
					}//end if else
				}//end if else
				
				switch(outcome){
				case 'w':
					System.out.println("White Player wins!");
					break;
				case 'b':
					System.out.println("Black Player wins!");
					break;
				case '-':
					System.out.println("The Game was a draw!");
					break;
				}//end switch case
				
			}while(outcome == 'a');//end do-while
			
			client.close();
			
		}catch(Exception e){
			System.out.println("Exception: " + e.getClass().getName());
			if(e.getMessage() != null){
				System.out.println("Error: " + e.getMessage());
			}//end if
		}//end try catch
	}//end playOnClient
	
	public static Move randomMove(State state, Random ran){
		ArrayList<Move> allMoves = state.moveGen();
		int r = ran.nextInt(allMoves.size());
		return allMoves.get(r);
	}//end method
	
	public static Move callBestMove(State state){
//		return state.newBestMove();
		state.bestMove();
		return state.m0;
	}//end method
	/*
	public static Move callNegamax(State state, Random ran){
		try{
			state.negamax(3, true);
		}catch(TimeOutException e){
//			
		}
		return state.m0;
	}//end 
	*/
}//end class
